<<<<<<< HEAD
dic_s = {"username": None, "status": False}


def dic_u():
    dic = {}
    with open("密码本", encoding="utf-8") as f1:
        for i in f1:
            dic[i.strip().split("|")[0]] = i.strip().split("|")[1]
    return dic


def enroll():
    dic = dic_u()
    while 1:
        user_name = input("请输入你的账号(只能含有字母或者数字不能含有特殊字符):").strip()
        if user_name.isalnum() and user_name not in dic:
            break
        print("账号不合法请重新输入")
    while 1:
        password = input("请输入你的密码(长度要在6~14个字符之间):").strip()
        if 4 <= len(password) <= 14:
            break
        print("密码长度不符合要求,请重新输入")
    with open("密码本", mode="a", encoding="utf-8") as f1:
        f1.write(f"\n{user_name}|{password}")


def login():
    dic = dic_u()
    count = 0
    while count < 3:
        user_name = input("请输入你的账号:").strip()
        password = input("请输入你的密码:").strip()
        if user_name in dic and password == dic[user_name]:
            dic_s["username"] = user_name
            dic_s["status"] = True
            return True
        count += 1
        print(f"账号或密码错误,错误次数{count}次")
    return False


def wrapper(f):
    def inner(*args,**kwargs):
        if dic_s["status"]:
            ret = f(*args,**kwargs)
            return ret
        else:
            print("请先登录")
            if login():
                ret = f(*args, **kwargs)
                return ret
            else:
                return exit()
    return inner


def choice():
    import os
    user_choice = int(input("""1.直接写入
2.导入文件"""))
    if user_choice == 1:
        user_write = input("请写入内容(文件名|文件内容)").strip()
        li = user_write.split("|", 1)
        with open(f"文章/{li[0]}", mode="w", encoding="utf-8") as f:
            f.write(li[1])
    elif user_choice == 2:
        user_lead = input("请输入要导入文件的路径")
        with open(user_lead, encoding="utf-8") as f1, \
                open(f"文章/{os.path.basename(user_lead)}", mode="w", encoding="utf-8") as f2:
            for i in f1:
                f2.write(i)


def com_next():
    import os
    li = os.listdir("G:\课上练习\大作业\第二次大作业(博客园)\文章")
    list_no = ["苍老师", "东京热", "武藤兰", "波多野结衣"]
    for i in range(len(li)):
        print(f"{i + 1} ：{li[i]}")
    choice_c = int(input("请选择您要评论的文章的序号"))
    with open(f"G:\课上练习\大作业\第二次大作业(博客园)\文章\{li[choice_c-1]}", encoding="utf-8") as f1:
        for i in f1:
            print(i)
    with open(f"G:\课上练习\大作业\第二次大作业(博客园)\文章\{li[choice_c - 1]}", mode="a", encoding="utf-8") as f1:
        comment = input("请输入你的评论:")
        for em in list_no:
            if em in comment:
                comment = comment.replace(em, len(em)*"*")
        write_in = f"""\n{"-"*15}评论区{"-"*15}
    (用户名){dic_s["username"]}
    {comment}"""
        f1.write(write_in)


@wrapper
def article():
    print(f'欢迎{dic_s["username"]}进入文章页面')
    choice()


def logout():
    dic_s["username"] = None
    dic_s["status"] = False


@wrapper
def comment():
    print(f'欢迎{dic_s["username"]}进入评论页面')
    com_next()


@wrapper
def diary():
    print(f'欢迎{dic_s["username"]}进入日记页面')


@wrapper
def collect():
    print(f'欢迎{dic_s["username"]}进入收藏页面')


def u_quit():
    return exit()


dic_func = {1: login, 2: enroll, 3: article, 4: comment,
            5: diary, 6: collect, 7: logout, 8: u_quit}


while 1:
    print("""1.请登录
2.请注册
3.进入文章页面
4.进入评论页面
5.进入日记页面
6.进入收藏页面
7.注销账号
8.退出整个程序""")
    num = int(input("请输入你的选择:"))
    if num == 1:
        if login():
            print("登录成功")
        else:
            break
    elif num == 7:
        if dic_s["status"]:
            logout()
        else:
            print("请先登录")
            login()
    else:
        dic_func[num]()
=======
dic_s = {"username": None, "status": False}


def dic_u():
    dic = {}
    with open("密码本", encoding="utf-8") as f1:
        for i in f1:
            dic[i.strip().split("|")[0]] = i.strip().split("|")[1]
    return dic


def enroll():
    dic = dic_u()
    while 1:
        user_name = input("请输入你的账号(只能含有字母或者数字不能含有特殊字符):").strip()
        if user_name.isalnum() and user_name not in dic:
            break
        print("账号不合法请重新输入")
    while 1:
        password = input("请输入你的密码(长度要在6~14个字符之间):").strip()
        if 4 <= len(password) <= 14:
            break
        print("密码长度不符合要求,请重新输入")
    with open("密码本", mode="a", encoding="utf-8") as f1:
        f1.write(f"\n{user_name}|{password}")


def login():
    dic = dic_u()
    count = 0
    while count < 3:
        user_name = input("请输入你的账号:").strip()
        password = input("请输入你的密码:").strip()
        if user_name in dic and password == dic[user_name]:
            dic_s["username"] = user_name
            dic_s["status"] = True
            return True
        count += 1
        print(f"账号或密码错误,错误次数{count}次")
    return False


def wrapper(f):
    def inner(*args,**kwargs):
        if dic_s["status"]:
            ret = f(*args,**kwargs)
            return ret
        else:
            print("请先登录")
            if login():
                ret = f(*args, **kwargs)
                return ret
            else:
                return exit()
    return inner


def choice():
    import os
    user_choice = int(input("""1.直接写入
2.导入文件"""))
    if user_choice == 1:
        user_write = input("请写入内容(文件名|文件内容)").strip()
        li = user_write.split("|", 1)
        with open(f"文章/{li[0]}", mode="w", encoding="utf-8") as f:
            f.write(li[1])
    elif user_choice == 2:
        user_lead = input("请输入要导入文件的路径")
        with open(user_lead, encoding="utf-8") as f1, \
                open(f"文章/{os.path.basename(user_lead)}", mode="w", encoding="utf-8") as f2:
            for i in f1:
                f2.write(i)


def com_next():
    import os
    li = os.listdir("G:\课上练习\大作业\第二次大作业(博客园)\文章")
    list_no = ["苍老师", "东京热", "武藤兰", "波多野结衣"]
    for i in range(len(li)):
        print(f"{i + 1} ：{li[i]}")
    choice_c = int(input("请选择您要评论的文章的序号"))
    with open(f"G:\课上练习\大作业\第二次大作业(博客园)\文章\{li[choice_c-1]}", encoding="utf-8") as f1:
        for i in f1:
            print(i)
    with open(f"G:\课上练习\大作业\第二次大作业(博客园)\文章\{li[choice_c - 1]}", mode="a", encoding="utf-8") as f1:
        comment = input("请输入你的评论:")
        for em in list_no:
            if em in comment:
                comment = comment.replace(em, len(em)*"*")
        write_in = f"""\n{"-"*15}评论区{"-"*15}
    (用户名){dic_s["username"]}
    {comment}"""
        f1.write(write_in)


@wrapper
def article():
    print(f'欢迎{dic_s["username"]}进入文章页面')
    choice()


def logout():
    dic_s["username"] = None
    dic_s["status"] = False


@wrapper
def comment():
    print(f'欢迎{dic_s["username"]}进入评论页面')
    com_next()


@wrapper
def diary():
    print(f'欢迎{dic_s["username"]}进入日记页面')


@wrapper
def collect():
    print(f'欢迎{dic_s["username"]}进入收藏页面')


def u_quit():
    return exit()


dic_func = {1: login, 2: enroll, 3: article, 4: comment,
            5: diary, 6: collect, 7: logout, 8: u_quit}


while 1:
    print("""1.请登录
2.请注册
3.进入文章页面
4.进入评论页面
5.进入日记页面
6.进入收藏页面
7.注销账号
8.退出整个程序""")
    num = int(input("请输入你的选择:"))
    if num == 1:
        if login():
            print("登录成功")
        else:
            break
    elif num == 7:
        if dic_s["status"]:
            logout()
        else:
            print("请先登录")
            login()
    else:
        dic_func[num]()
>>>>>>> 00
